/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: Anonymous
 * License Type: Purchased
 */
package com.estate.datalogic.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import com.estate.datalogic.*;

/**
 * ORM-Persistable Class
 */
public class Ad_typeDAOImpl implements com.estate.datalogic.dao.Ad_typeDAO {
	private static final org.apache.log4j.Logger _logger = org.apache.log4j.Logger.getLogger(Ad_typeDAOImpl.class);
	public Ad_type loadAd_typeByORMID(int idAd_type) throws PersistentException  {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return loadAd_typeByORMID(session, idAd_type);
	}
	
	public Ad_type loadAd_typeByORMID(PersistentSession session, int idAd_type) throws PersistentException {
		return (Ad_type) session.load(com.estate.datalogic.Ad_type.class, new Integer(idAd_type));
	}
	
	public Ad_type[] listAd_typeByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return listAd_typeByQuery(session, condition, orderBy);
	}
	
	public Ad_type[] listAd_typeByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From com.estate.datalogic.Ad_type as Ad_type");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			List list = query.list();
			return (Ad_type[]) list.toArray(new Ad_type[list.size()]);
		}
		catch (Exception e) {
			_logger.error("listAd_typeByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Ad_type loadAd_typeByQuery(String condition, String orderBy) throws PersistentException {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return loadAd_typeByQuery(session, condition, orderBy);
	}
	
	public Ad_type loadAd_typeByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Ad_type[] ad_types = listAd_typeByQuery(session, condition, orderBy);
		if (ad_types != null && ad_types.length > 0)
			return ad_types[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateAd_typeByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return iterateAd_typeByQuery(session, condition, orderBy);
	}
	
	public static java.util.Iterator iterateAd_typeByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From com.estate.datalogic.Ad_type as Ad_type");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			return query.iterate();
		}
		catch (Exception e) {
			_logger.error("iterateAd_typeByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Ad_type createAd_type() {
		return new com.estate.datalogic.Ad_type();
	}
	
	public boolean save(com.estate.datalogic.Ad_type ad_type)throws PersistentException {
		try {
			com.estate.datalogic.EstatePersistentManager.instance().saveObject(ad_type);
			return true;
		}
		catch (Exception e) {
			_logger.error("save(com.estate.datalogic.Ad_type ad_type)", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(com.estate.datalogic.Ad_type ad_type)throws PersistentException {
		try {
			com.estate.datalogic.EstatePersistentManager.instance().deleteObject(ad_type);
			return true;
		}
		catch (Exception e) {
			_logger.error("delete(com.estate.datalogic.Ad_type ad_type)", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(com.estate.datalogic.Ad_type ad_type)throws PersistentException {
		try {
			com.estate.datalogic.Advertisement[] lAdvertisements = ad_type.advertisement.toArray();
			for(int i = 0; i < lAdvertisements.length; i++) {
				lAdvertisements[i].setAd_typeidAd_type(null);
			}
			return delete(ad_type);
		}
		catch(Exception e) {
			_logger.error("deleteAndDissociate()", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(com.estate.datalogic.Ad_type ad_type)throws PersistentException {
		try {
			com.estate.datalogic.EstatePersistentManager.instance().getSession().refresh(ad_type);
			return true;
		}
		catch (Exception e) {
			_logger.error("refresh(com.estate.datalogic.Ad_type ad_type)", e);
			throw new PersistentException(e);
		}
	}
	
}
